/**
 * 文 件 名:  NoticeContorller.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.Manager;
import com.czd.support.model.Notice;
import com.czd.support.model.Site;
import com.czd.support.service.NoticeService;
import com.czd.support.service.SiteService;

/**
 * 公告控制类
 * 
 * @Author Jun.Yang
 * @Date 2015年2月8日 下午2:11:28
 * @Version 1.0
 */
@Controller
@RequestMapping("/notice")
public class NoticeContorller extends BaseController {
	@Autowired
	private NoticeService noticeService;
	@Autowired
	private SiteService siteService;

	@ModelAttribute("initSiteList")
	private List<Site> initSiteList() {
		return siteService.findSiteList();
	}
	
	/**
	 * 公告列表
	 * @param notice
	 * @param pageNo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"","/","/list"})
	public String list(
			Notice notice,
			@RequestParam(value = "pageNo", defaultValue = "1", required = false) int pageNo,
			Model model) {
		notice.setPageNo(pageNo);
		notice.setPageSize(Configuration.getInstance().getDefault_page_size());

		Paginate paginate = noticeService.queryForPage(notice);

		model.addAttribute("paginate", paginate);

		return "notice/list";
	}

	/**
	 * 跳转到公告信息添加页面
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return "notice/add";
	}

	/**
	 * 公告信息添加
	 * @param request
	 * @param notice
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, Notice notice, Model model) {
		Manager manager = this.getSesionUser(request);
		notice.setCreateUser(manager.getUserCode());
		notice.setUpdateUser(manager.getUserCode());

		if (noticeService.insert(notice)) {
			model.addAttribute("notice", notice);
			return "redirect:/notice/" + notice.getId() + "/details";
		}

		return error(model, "操作失败", "添加公告信息失败");
	}

	/**
	 * 跳转到公告信息编辑
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		Notice notice = noticeService.findById(id);
		
		if(notice != null) {
			model.addAttribute("notice", notice);
			
			return "notice/edit";
		}
		
		return error(model, "操作失败", "公告信息不存在");
	}

	/**
	 * 公告信息编辑
	 * @param request
	 * @param notice
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, Notice notice, Model model) {
		Manager manager = this.getSesionUser(request);
		notice.setUpdateUser(manager.getUserCode());

		if (noticeService.update(notice)) {
			model.addAttribute("notice", notice);
			return "redirect:/notice/" + notice.getId() + "/details";
		}

		return error(model, "操作失败", "修改公告信息失败");
	}

	
	@RequestMapping(value = "{id}/delete", method = RequestMethod.GET) 
	public String delete(@PathVariable int id, Model model) {
		if(noticeService.delete(id)) {
			return "redirect: /notice/list";
		}
		
		return error(model, "操作失败", "删除公告信息失败");
	}
	
	/**
	 * 公告信息详情
	 * @param request
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(HttpServletRequest request, @PathVariable int id,
			Model model) {
		Notice notice = noticeService.findById(id);

		if (notice != null) {
			model.addAttribute("notice", notice);
			return "notice/details";
		}

		return error(model, "操作失败", "公告信息不存在");
	}
}
